/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package central;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Martin
 */
public class Guardar {

    private String archivo= "Capturas.txt";
    private static Guardar instance = new Guardar();
    private FileWriter fw;

    public FileWriter getFw() {
        return fw;
    }

    public void setFw(FileWriter fw) {
        this.fw = fw;
    }

    public static Guardar getInstance() {
        return instance;
    }

    private Guardar() {
    }

    public void persistirCapturas(String linea) {
        try {
            List<String> leer = leerCapturas(archivo);
            fw = new FileWriter(archivo);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter salida = new PrintWriter(bw);
            for (String text : leer) {
                salida.println(text);
            }
            salida.println(linea);
            salida.close();
        } catch (java.io.IOException ioex) {
            System.out.println("se presento error: " + ioex.toString());
        }
    }

    public List<String> leerCapturas(String txt) throws IOException {
        ArrayList<String> text = new ArrayList<String>();
        try {
            FileReader fr = new FileReader(txt);
            BufferedReader entrada = new BufferedReader(fr);
            String s;
            while ((s = entrada.readLine()) != null) {
                text.add(s);
            }
        } catch (java.io.FileNotFoundException fnfex) {
            System.out.println("se presento el error: " + fnfex.toString());
        }
        return text;
    }
}
